<?php
abstract class system_controller_action {
	public static $viewSuffix = 'phtml';
	protected $_helper;	
	/**
	 * @var system_view_abstract
	 */
	public $view;	
	/**
	 * @var system_view_abstract
	 */
	public $layout;	
	protected $_request;
	protected $_reponse;
	
	public function __construct(system_controller_request_abstract $request, system_controller_response_abstract $response) {
		$this->setRequest($request);
		$this->setResponse($response);
		$this->_helper = new system_controller_action_helper($this);
		$this->initView();
		$this->init();
	}
	
	/**
	 * @return system_controller_action_helper
	 */
	public function __call($method, $args) {
		return call_user_func(array($this->_helper, $method), $args);
	}
	public function __get($name) {
		return $this->_helper->{$name};
	}
	
	protected function init() {
	}
	
	/**
	 * @return system_controller_action
	 */
	public function setRequest(system_controller_request_abstract $request) {
		$this->_request = $request;
		return $this;
	}
	
	/**
	 * @return system_controller_request_Http
	 */
	public function getRequest() {
		if(null === $this->_request) {
			$this->setRequest(system_controller_front::getInstance()->getRequest());
		}
		return $this->_request;
	}
	
	/**
	 * @return system_controller_action
	 */
	public function setResponse(system_controller_response_abstract $response) {
		$this->_response = $response;
		return $this;
	}
	
	/**
	 * @return system_controller_response_http
	 */
	public function getResponse() {
		if(null === $this->_response) {
			$this->setResponse(system_controller_front::getInstance()->getResponse());
		}
		return $this->_response;
	}
	
	public function preDispatch() {
	}
	public function postDispatch() {
	}
	public function dispatch($action) {
		$this->preDispatch();
		if($this->getRequest()->isDispatched()) {
			$this->$action();
			
			$this->postDispatch();
		}
	}
	
	/**
	 * @return system_view_abstract
	 */
	public function initView() {
		if(null === $this->view) {
			$this->view = system_view::getControllerActionViewInstance();
		}
		if(null === $this->layout) {
			$layout = system_view_layout::getInstance();
			$layout->setView($this->view);
			$this->_layout = $layout;
		}
		return $this->view;
	}
	
	/**
	 * @return system_controller_action
	 */
	public function setView(system_view_interface $view) {
		$this->view = $view;
		return $this;
	}
	
	/**
	 * @return system_view_layout
	 */
	public function layout() {
		if(null == $this->view) {
			$this->initView();
		}
		return $this->_layout;
	}
	
	public function render($script = null, $name = null) {
		if(null === $script) {
			$action = $this->getRequest()->getActionName();
			$controller = $this->getRequest()->getControllerName();			
			$script = str_replace('_', DIRECTORY_SEPARATOR, $controller) . DIRECTORY_SEPARATOR . $action . '.' . self::$viewSuffix;
		}
		$view = $this->initView();
		$response = $this->getResponse();
		$content = $view->render($script);		
		if($this->layout()->isEnabled()) {
			$this->layout()->getView()->content = $content;
			$content = $this->layout()->render();
		}		
		$response->appendBody($content, $name);
	}
	
	final protected function _forward($action, $controller = null, array $params = null) {
		$request = $this->getRequest();		
		if(null !== $params) {
			$request->setParams($params);
		}		
		if(null !== $controller) {
			$request->setControllerName($controller);
		}		
		$request->setActionName($action);
		$request->setDispatched(false); //关闭显示
	}
	
	protected function _redirector($url, array $options = array()) {
		return $this->_helper->redirector->gotoUrl($url, $options);
	}

}